package com.cnpinxi.controller.admin.product;

import com.cnpinxi.controller.base.BaseController;
import com.cnpinxi.def.Constant;
import com.cnpinxi.domain.PackageRelation;
import com.cnpinxi.result.ResultObj;
import com.cnpinxi.service.PackageRelationService;
import com.cnpinxi.util.StringUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * Created by Baggio on 2017-11-16.
 */
@RequestMapping("/admin/package")
@Controller
public class AdminPackageController   extends BaseController {
@Autowired
PackageRelationService packageRelationService;
    @RequestMapping("/getPackageRelationList")
    public ModelAndView getPackageRelationList(HttpServletRequest request, Integer page, Integer pageSize) {
        ModelAndView m = new ModelAndView("/admin/product/getPackageRelationList");
        Map<String, String> map = this.showRequestParameters(request);
        if (page == null || page == 0) {
            page = Constant.PAGENUM;
        }
        // 设置每页记录数
        if (pageSize == null || pageSize == 0) {
            pageSize = Constant.PAGESIZENum;
        }
        map.put("page", String.valueOf(page));
        map.put("pageSize", String.valueOf(pageSize));
        List<PackageRelation> packageRelationList = packageRelationService.getPackageRelationList(map);
        PageInfo<PackageRelation> pageinfo = new PageInfo<>(packageRelationList);
        m.addObject("pageinfo", pageinfo);
        m.addObject("bean", map);
        return m;
    }

    @RequestMapping(value = "/packageRelation")
    public ModelAndView packageRelation(HttpServletRequest request){
        ModelAndView m = new ModelAndView("/admin/product/packageRelation");
        Map<String, String> map = this.showRequestParameters(request);
        String tid=map.get("tid");
        if(StringUtil.isNotEmpty(tid)){
            PackageRelation packageRelation=  packageRelationService.getPackageRelation(map);
            m.addObject("packageRelation",packageRelation);
        }

        m.addObject("bean",map);

        return m;
    }
    /**
     * 编辑资源提交
     * @param packageRelation
     * @return
     */
    @RequestMapping(value = "/modifypackageRelation")
    @ResponseBody
    public String modifypackageRelation(HttpServletRequest request,PackageRelation packageRelation){
        ResultObj resultObj = new ResultObj();
        resultObj.setCode(ResultObj.CODE_SUCCESS);
        try {
            packageRelationService.modifyPackageRelation(packageRelation);
            resultObj.setMsg("操作成功！");
        } catch (Exception e) {
            resultObj.setCode(ResultObj.CODE_FAIL);
            resultObj.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return resultObj.toJson();
    }/**
     * 编辑资源提交
     * @param id
     * @return
     */
    @RequestMapping(value = "/deletePackageRelation")
    @ResponseBody
    public String deletePackageRelation(Long id){
        ResultObj resultObj = new ResultObj();
        resultObj.setCode(ResultObj.CODE_SUCCESS);
        try {
            packageRelationService.deletePackageRelationById(id);
            resultObj.setMsg("操作成功！");
        } catch (Exception e) {
            resultObj.setCode(ResultObj.CODE_FAIL);
            resultObj.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return resultObj.toJson();
    }
}
